<?php
    class MailController extends ControllerBase{
        public function indexAction(){
            if($this->request->isPost()){
                $data=$this->request->getPost();
                $condition="account='".$data['account']."'";
                $user=User::findFirst(array($condition));
                if($user->id>0){
                    $info['mail_code']=substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 10);
                    $info['mail_time']=time();
                    if($user->save($info)){
                        $config=Config::findFirst();
                        $smtpServer = "smtp.163.com";
                        $smtpserverport =25;
                        $smtpusermail = "15939025192@163.com";
                        $smtpemailto = $user->email;
                        $smtpuser = "15939025192@163.com";
                        $smtppass = "zhi0371wei";
                        $mailtitle = "右右网-重置密码";
                        $mailcontent = "此链接10分钟内有效，请点击下面的链接修改密码：<br/><a href='".$config->domain."mail/changepassword/".$info['mail_code']."' target='_blank'>点我修改密码</a>";
                        $mailtype = "HTML";
                        $smtp = new Smtp($smtpServer,$smtpserverport,true,$smtpuser,$smtppass);
                        $smtp->debug = false;
                        $state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
                        if($state==""){
                            echo "<script>alert('邮件发送失败，请稍候重试！');location='/';</script>";
                            exit;
                        }
                        echo "<script>alert('邮件已成功发送至您注册时的邮箱中，请注意查收！');location='/';</script>";
                        exit;
                    }else{
                        echo "<script>alert('操作失败，请稍候重试！');location='/';</script>";
                        exit;
                    }
                }else{
                    echo "<script>alert('此用户不存在！');location='/';</script>";
                    exit;
                }
            }
        }
        public function changepasswordAction($id){
            $condition="mail_code='".$id."'"." and ".time()."<mail_time+600";
            $user=User::findFirst(array($condition));
            if($user->id>0){
                if($this->request->isPost()){
                    $data=$this->request->getPost();
                    if(!preg_match('/^[\w]{6,32}$/',$data['password'])){
                        echo "<script>alert('密码格式有误，请重新输入！');location='/mail/changepassword/".$id."';</script>";
                        exit;
                    }
                    if($data['password']!=$data['repassword']){
                        echo "<script>alert('两次密码输入不一致，请重新输入！');location='/mail/changepassword/".$id."';</script>";
                        exit;
                    }
                    $data['password']=sha1($data['password']);
                    if($user->save(array("password"=>$data['password'],"mail_time"=>0,"mail_code"=>"0"))){
                        echo "<script>alert('密码修改成功，请登录！');location='/';</script>";
                        exit;
                    }else{
                        echo "<script>alert('密码修改失败，请稍候重试！');location='/';</script>";
                        exit;
                    }
                }
            }else{
                echo "<script>alert('链接异常或此链接已失效！');location='/';</script>";
                exit;
            }
        }
    }